package com.gxs.tga.poc.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gxs.tga.poc.domain.Test;
import com.gxs.tga.poc.domain.exceptions.TestNotFound;
import com.gxs.tga.poc.repository.TestRepository;

@Service
public class TestServiceImpl implements TestService
{

	@Resource
	private TestRepository TestRepository;

	@Override
	@Transactional
	public Test create(Test test)
	{
		Test createdTest = test;
		return TestRepository.save(createdTest);
	}

	@Override
	@Transactional
	public Test findById(Long id)
	{
		return TestRepository.findOne(id);
	}

	@Override
	@Transactional(rollbackFor = TestNotFound.class)
	public Test delete(Long id) throws TestNotFound
	{
		Test deletedTest = TestRepository.findOne(id);

		if (deletedTest == null)
			throw new TestNotFound();

		TestRepository.delete(deletedTest);
		return deletedTest;
	}

	@Override
	@Transactional
	public List<Test> findAll()
	{
		return TestRepository.findAll();
	}

	@Override
	@Transactional(rollbackFor=TestNotFound.class)
	public Test update(Test test) throws TestNotFound {
		Test updatedTest = TestRepository.findOne(test.getId());
		
		if (updatedTest == null)
			throw new TestNotFound();
		
		updatedTest.setName(test.getName());
		updatedTest.setComments(test.getComments());
		return updatedTest;
	}
}
